package shop.core.common.oscache;

import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import shop.core.cache.CacheProvider;
import shop.core.cache.SimpleCacheProvider;
import shop.core.common.bean.AccessToken;
import shop.core.common.listener.SystemListener;
import shop.services.front.area.bean.Area;
import shop.services.front.catalog.bean.Catalog;
import shop.services.front.express.bean.Express;
import shop.services.front.indexImg.bean.IndexImg;
import shop.services.front.news.bean.News;
import shop.services.front.product.bean.ProductStockInfo;
import shop.services.manage.accountRank.bean.AccountRank;
import shop.services.manage.activity.bean.Activity;
import shop.services.manage.systemSetting.bean.SystemSetting;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;


/**
 * 系统管理类.
 * 1、负责管理system.properties的东东
 * 2、负责缓存管理
 *
 * @author huangf
 */
public class SystemManager {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SystemManager.class);
    private static Properties p = new Properties();
    private static CacheProvider cacheProvider = new SimpleCacheProvider();
    private static SystemManager instance;

    @PostConstruct
    public void afterPropertiesSet() {
        instance = this;
    }

    private static Map<String, String> manageExpressMap = new HashMap<String, String>();//后台发货页面物流公司列表

    public static SystemManager getInstance() {
        return instance;
    }

    static {
        init();
    }

    private static void init() {
        try {
            p.load(SystemListener.class.getResourceAsStream("/system.properties"));
            logger.info(p.toString());
        } catch (IOException e) {
            logger.error(e.toString());
        }
    }

    public String getProperty(String key) {
        return p.getProperty(key);
    }


    /**
     * 根据类别ID获取该类别下的所有ID集合
     *
     * @param catalogID
     * @return
     */
    public List<String> getCatalogsById(String catalogID) {
        if (StringUtils.isBlank(catalogID)) {
            return null;
        }

        List<Catalog> catalogs = getCatalogs();
        if (catalogs == null || catalogs.size() == 0) {
            return null;
        }

        List<String> list = new LinkedList<String>();
        Catalog cataInfo = getCatalogsMap().get(catalogID);
        if (cataInfo.getPid().equals("0")) {
            //主类
            for (int i = 0; i < cataInfo.getChildren().size(); i++) {
                list.add(cataInfo.getChildren().get(i).getId());
            }
        } else {
            //子类
            list.add(catalogID);
        }
        return list;
    }


    //应用缓存管理

    public void setCacheProvider(CacheProvider cacheProvider) {
        SystemManager.cacheProvider = cacheProvider;
    }

    private static String buildKey(String key) {
        return "SystemManager." + StringUtils.trimToEmpty(key);
    }

    private static void putCacheObject(String key, Serializable cacheObject) {
        String key1 = buildKey(key);
        if (cacheObject == null) {
            cacheProvider.remove(key1);
        } else {
            cacheProvider.put(key1, cacheObject);
        }
    }

    private static <T extends Serializable> T getCacheObject(String key) {
        return (T) cacheProvider.get(buildKey(key));
    }

    /**
     * 产品目录列表
     *
     * @return
     */
    public List<Catalog> getCatalogs() {
        return getCacheObject("catalogs");
    }

    public void setCatalogs(List<Catalog> catalogs) {
        putCacheObject("catalogs", (Serializable) (catalogs));
    }

    /**
     * 文章目录列表
     *
     * @return
     */
    public List<Catalog> getCatalogsArticle() {
        return getCacheObject("catalogsArticle");
    }

    public void setCatalogsArticle(List<Catalog> catalogsArticle) {
        putCacheObject("catalogsArticle", (Serializable) catalogsArticle);
    }


    /**
     * 文章列表
     *
     * @return
     */
    public Map<String, News> getNewsMap() {
        return getCacheObject("newsMap");
    }

    public void setNewsMap(Map<String, News> newsMap) {
        putCacheObject("newsMap", (Serializable) newsMap);
    }


    //系统设置
    public SystemSetting getSystemSetting() {
        return getCacheObject("systemSetting");
    }

    public void setSystemSetting(SystemSetting systemSetting) {
        putCacheObject("systemSetting", systemSetting);
    }


    /**
     * 会员等级列表
     *
     * @return
     */
    public Map<String, AccountRank> getAccountRankMap() {
        return getCacheObject("accountRankMap");
    }

    public void setAccountRankMap(Map<String, AccountRank> accountRankMap) {
        putCacheObject("accountRankMap", (Serializable) accountRankMap);
    }



    /**
     * 目录的MAP形式，具有层级关系。key：主类别ID，value：主类别对象，可以通过该对象的getChildren()方法获取该主类别的所有的子类别集合
     *
     * @return
     */
    public Map<String, Catalog> getCatalogsMap() {
        return getCacheObject("catalogsMap");
    }

    public void setCatalogsMap(Map<String, Catalog> catalogsMap) {
        putCacheObject("catalogsMap", (Serializable) catalogsMap);
    }

    /**
     * 目录的MAP形式，具有层级关系。key：主类别code，value：主类别对象，可以通过该对象的getChildren()方法获取该主类别的所有的子类别集合
     *
     * @return
     */
    public Map<String, Catalog> getCatalogsCodeMap() {
        return getCacheObject("catalogsCodeMap");
    }

    public void setCatalogsCodeMap(Map<String, Catalog> catalogsCodeMap) {
        putCacheObject("catalogsCodeMap", (Serializable) catalogsCodeMap);
    }

    /**
     * 商品库存信息,应该使用JAVA类库中的读写锁，key:商品ID，value：商品对象
     *
     * @return
     */
    public Map<String, ProductStockInfo> getProductStockMap() {
        return getCacheObject("productStockMap");
    }

    public void setProductStockMap(Map<String, ProductStockInfo> productStockMap) {
        putCacheObject("productStockMap", (Serializable) productStockMap);
    }



    /**
     * 门户滚动图片
     *
     * @return
     */
    public List<IndexImg> getIndexImages() {
        return getCacheObject("indexImages");
    }

    public void setIndexImages(List<IndexImg> indexImages) {
        putCacheObject("indexImages", (Serializable) (indexImages));
    }


    /**
     * 省市区集合
     *
     * @return
     */
    public Map<String, Area> getAreaMap() {
        return getCacheObject("areaMap");
    }

    public void setAreaMap(Map<String, Area> areaMap) {
        putCacheObject("areaMap", (Serializable) areaMap);
    }

    /**
     * 城市列表
     *
     * @return
     */
    public List<Area> getCityList() {
        return getCacheObject("cityAllList");
    }

    public void setCityList(List<Area> cityList) {
        putCacheObject("cityAllList", (Serializable) cityList);
    }


    /**
     * 前台订单支付页面--物流列表
     *
     * @return
     */
    public Map<String, Express> getExpressMap() {
        return getCacheObject("expressMap");
    }

    public void setExpressMap(Map<String, Express> expressMap) {
        putCacheObject("expressMap", (Serializable) (expressMap));
    }


    /**
     * 后台发货页面物流公司列表
     *
     * @return
     */
    public Map<String, String> getManageExpressMap() {
        Map<String, String> cachedMap = getCacheObject("manageExpressMap");
        if (cachedMap != null) {
            return cachedMap;
        }
        return SystemManager.manageExpressMap;
    }

    public void setManageExpressMap(Map<String, String> manageExpressMap) {
        putCacheObject("manageExpressMap", Lists.newArrayList(manageExpressMap));
    }


    /**
     * 所有活动列表
     *
     * @return
     */
    public Map<String, Activity> getActivityMap() {
        return getCacheObject("activityMap");
    }

    public void setActivityMap(Map<String, Activity> activityMap) {
        putCacheObject("activityMap", (Serializable) (activityMap));
    }


    /////////////////后台类目查询的JSON字符串缓存///////////////////

    /**
     * 商品类目JSON字符串缓存
     *
     * @return
     */
    public String getProductCatalogJsonStr() {
        return getCacheObject("productCatalogJsonStr");
    }

    public void setProductCatalogJsonStr(String productCatalogJsonStr) {
        putCacheObject("productCatalogJsonStr", productCatalogJsonStr);
    }

    /**
     * 缓存类目JSON字符串缓存
     *
     * @return
     */
    public String getArticleCatalogJsonStr() {
        return getCacheObject("articleCatalogJsonStr");
    }

    public void setArticleCatalogJsonStr(String articleCatalogJsonStr) {
        putCacheObject("articleCatalogJsonStr", articleCatalogJsonStr);
    }


    public AccessToken getAccessToken() {
        return getCacheObject("accessToken");
    }

    public void setAccessToken(AccessToken accessToken) {
        putCacheObject("accessToken", (Serializable)accessToken);
    }

}
